using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web;

namespace Common
{
    public class JavaScript
    {
        private Page Pg;
        private bool IsInAsyncPostBack;
        private bool IsIE;
        private bool IsWaitForPageLoad;

        private JavaScript(Page pageInstance, bool waitLoad)
        {
            Pg = pageInstance;
            IsInAsyncPostBack = ScriptManager.GetCurrent(Pg).IsInAsyncPostBack;
            IsIE = HttpContext.Current.Request.Browser.Browser == "IE";
            IsWaitForPageLoad = waitLoad;
        }

        /// <summary>
        /// Execute scripts before page load
        /// </summary>
        public static JavaScript BeforePageLoad(Page Page)
        {
            return new JavaScript(Page, false);
        }

        /// <summary>
        /// Execute scripts after page loaded
        /// </summary>
        public static JavaScript AfterPageLoad(Page Page)
        {
            return new JavaScript(Page, true);
        }

        private void RegisterScript(string scriptText)
        {
            if (IsWaitForPageLoad && !IsInAsyncPostBack)
            {
                if (IsIE)
                    scriptText = "window.attachEvent('onload', function() {" + scriptText + "});";
                else
                    scriptText = "window.addEventListener('load', function() {" + scriptText + "}, false);";
            }
            if (!IsWaitForPageLoad && !IsInAsyncPostBack)
                ScriptManager.RegisterClientScriptBlock(Pg, Pg.GetType(), Guid.NewGuid().ToString(), scriptText, true);
            else
                ScriptManager.RegisterStartupScript(Pg, Pg.GetType(), Guid.NewGuid().ToString(), scriptText, true);
        }

        /// <summary>
        /// Show message box
        /// </summary>
        public JavaScript Alert(string Message, params object[] args)
        {
            if (args != null && args.Length > 0)
                Message = String.Format(Message, (object[])args);

            Message = Message.Replace("\r", "");
            Message = Message.Replace("\n", "\\n");
            Message = Message.Replace("'", "\\'");
            RegisterScript(GetConfirmScript() + "alert('" + Message + "');");
            return this;
        }


        /// <summary>
        /// Redirect to another location
        /// </summary>
        public JavaScript Redirect(string Url, params object[] args)
        {
            if (args != null && args.Length > 0)
                Url = String.Format(Url, (object[])args);
            RegisterScript(GetConfirmScript() + "window.location.replace('" + Url + "');");
            return this;
        }

        /// <summary>
        /// Reload current page with out postback
        /// </summary>
        public JavaScript Reload()
        {
            RegisterScript(GetConfirmScript() + "window.location.replace(window.location.href);");
            return this;
        }

        /// <summary>
        /// Close the window
        /// </summary>
        public JavaScript ClosePopupWindow()
        {
            RegisterScript(GetConfirmScript() + "window.close();");
            return this;
        }

        /// <summary>
        /// Execute javascript literal, Confirm script will be ignored
        /// </summary>
        public JavaScript ExecuteCustomScript(string scriptText, params object[] args)
        {
            if (args != null && args.Length > 0)
                scriptText = String.Format(scriptText, (object[])args);
            RegisterScript(scriptText);
            return this;
        }

        /// <summary>
        /// Execute javascript in eval(); function
        /// </summary>
        public JavaScript Eval(string scriptText, params object[] args)
        {
            if (args != null && args.Length > 0)
                scriptText = String.Format(scriptText, (object[])args);
            RegisterScript(GetConfirmScript() + "eval(" + scriptText + ");");
            return this;
        }

        private string confirmScript = String.Empty;
        private void Confirm(bool BooleanExpr, string Message, params object[] args)
        {
            if (args != null && args.Length > 0)
                Message = String.Format(Message, (object[])args);

            Message = Message.Replace("\r", "");
            Message = Message.Replace("\n", "\\n");
            Message = Message.Replace("'", "\\'");
            confirmScript = "if (" + (BooleanExpr ? "" : "!") + "confirm('" + Message + "')) ";
        }

        /// <summary>
        /// Confirm user for action after
        /// </summary>
        public JavaScript Confirm(string Message, params object[] args)
        {
            Confirm(true, Message, (object[])args);
            return this;
        }

        /// <summary>
        /// Confirm user for next commands
        /// </summary>
        public JavaScript ConfirmNot(string Message, params object[] args)
        {
            Confirm(false, Message, (object[])args);
            return this;
        }

        private string GetConfirmScript()
        {
            if (confirmScript.Length > 0)
            {
                string cfm = confirmScript;
                confirmScript = String.Empty;
                return cfm;
            }
            return String.Empty;
        }

    }
}
